package com.tanhua.sso.controller;

import com.tanhua.sso.service.UserInfoService;
import com.tanhua.sso.vo.ErrorResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("user")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 完善个人信息 - 基本信息
     * @param map  用户信息
     * @param token
     * @return
     */
    @PostMapping("loginReginfo")
    public ResponseEntity<Object> saveUserInfo(@RequestBody Map<String,String> map ,
                                               @RequestHeader("Authorization") String token){
        //调用保存方法
        Boolean flag = userInfoService.saveUserInfo(map,token);
        //如果返回是true, 则表示保存成功, 响应200
        if (flag){
            return ResponseEntity.ok(null);
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000003").errMessage("保存用户信息失败").build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    /**
     * 完善个人信息 - 用户头像
     * @param file  用户头像文件
     * @param token
     * @return
     */
    @PostMapping("loginReginfo/head")
    public ResponseEntity<Object> saveUserLogo(@RequestParam("headPhoto") MultipartFile file,
                                               @RequestHeader("Authorization") String token){
        //调用保存方法
        Boolean flag = userInfoService.saveUserLogo(file,token);
        //如果返回是true, 则表示保存成功, 响应200
        if (flag){
            return ResponseEntity.ok(null);
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000003").errMessage("保存用户头像失败").build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }
}
